package media.sound;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class SoundEffectManager {
	
	private  SoundPool mSoundPool; 
	private  HashMap<Integer, Integer> mSoundPoolMap; // Sound ID
	private  AudioManager  mAudioManager;
	private  int mStreamID;							  // StreamID
	private  boolean IsLoop = false;
	private  Context mContext;

		
	public void initSounds(Context theContext) { 
		 mContext = theContext;
		
	     mSoundPool = new SoundPool(5,  AudioManager.STREAM_MUSIC, 0); 	     
	     mSoundPoolMap = new HashMap<Integer, Integer>(); 
	     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE); 	     
	} 
	
	public void addSound(int Index,int SoundID)
	{

		mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1)); // Gio han khoang < 1MB
		

	}
	
	public void playSound(int index) { 
		
	     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
	     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
	    

	}
	
	public void playLoopedSound(int index) { 
		if(IsLoop == false)
		{
		     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
		     mStreamID = mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f); 
		     IsLoop = true;
		}
	   
	    
	}
	
	public void StopLoop()
	{
		if(IsLoop == true)
		{
			mSoundPool.stop(mStreamID);
			IsLoop = false;			
		}	
	}
}
